﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Factories;
using Microsoft.Xna.Framework.Input;



namespace XNA_2DFramwork
{
    public class Entity : VisualObject
    {
        private Rectangle r;
        public Entity(Game Game, Vector2 position,int width, int height, string filename, GraphicsDevice gd, PhysicsSimulator physicsSimulator)
            : base(Game)
        {
            PhysicsSimulator = physicsSimulator;
            Position = position;
            //---------------------------------------------------------------------------------------
            Texture = Game.Content.Load<Texture2D>(filename);
            
            Width = width;
            Height = height;
            Origin = new Vector2(Width / 2f, Height / 2f);
            SpriteBatch = new SpriteBatch(gd);
            Body = BodyFactory.Instance.CreateRectangleBody(PhysicsSimulator, Width, Height, 1);
            Body.Position = Position;
            Geom = GeomFactory.Instance.CreateRectangleGeom(PhysicsSimulator, Body, Width, Height);
            Geom.FrictionCoefficient = 1;
            r = new Rectangle(0, 0, Width, Height);
        }
    
        public override void Draw()
        {
            SpriteBatch.Begin();
            SpriteBatch.Draw(Texture, Body.Position, r, Color.White, Body.Rotation, Origin, 1f, SpriteEffects.None,
                             0f);
            SpriteBatch.End();
        }

        public override void Update()
        {
            if (Keyboard.GetState().IsKeyDown(Keys.J))
                Jump(new Vector2(500, 500));
            if (Keyboard.GetState().IsKeyDown(Keys.A))
                Body.ApplyForce(new Vector2(-400, 0));
            if (Keyboard.GetState().IsKeyDown(Keys.W))
                Body.ApplyForce(new Vector2(0, -400));
            if (Keyboard.GetState().IsKeyDown(Keys.S))
                Body.ApplyForce(new Vector2(0, 400));
            if (Keyboard.GetState().IsKeyDown(Keys.D))
                Body.ApplyForce(new Vector2(400, 0));
            if (Keyboard.GetState().IsKeyDown(Keys.Left))
                Walk(new Vector2(Body.Position.X + 50, Body.Position.Y));
            // Body.ApplyTorque(100f);
            if (Keyboard.GetState().IsKeyDown(Keys.Right))
                Body.ApplyTorque(-100f);

        }


    }
}